package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.dto.TProcessDto;
import com.woniu.car.dto.constant.CommonConstant;
import com.woniu.car.entity.TProcess;
import com.woniu.car.entity.TService;
import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.mapper.TProcessMapper;
import com.woniu.car.param.TProcessParam;
import com.woniu.car.param.TProcessUpdateParam;
import com.woniu.car.service.TProcessService;
import com.woniu.car.service.TServiceService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务流程表 服务实现类
 * </p>
 *
 * @author *********
 * @since 2021-06-21
 */
@Service
public class TProcessServiceImpl extends ServiceImpl<TProcessMapper, TProcess> implements TProcessService {

    @Resource
    private TServiceService tServiceService;

    /**
    *@Author 李磊
    *@Description 服务流程新增
    *@Param [tProcessParam, id]
    *@return com.woniu.car.dto.TProcessDto
    **/
    @Override
    @Transactional
    public TProcessDto insert(TProcessParam tProcessParam, Integer id) throws Exception {

        TService byId1 = tServiceService.getById(tProcessParam.getServiceId());

        if (null == byId1) {
            throw new ParamException(CommonConstant.COWRONG);
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("service_name",tProcessParam.getServiceName());
        List<TProcess> list = this.list(wrapper);

        for (TProcess tProcess : list) {
            if (null != tProcess && tProcessParam.getServiceId().equals(tProcess.getServiceId())) {
                throw new ParamException(CommonConstant.PROCESSNAMEWRONG);
            }
        }

        TProcess process = new TProcess();
        String s = UUID.randomUUID().toString();
        BeanUtils.copyProperties(tProcessParam,process);
        process.setUserId(id);
        process.setCreateNo(s);

        boolean save = this.save(process);

        if (!save) {
            throw new ServiceSException(CommonConstant.INSERTWRONG);
        }

        TProcess byId = this.getById(process.getProcessId());

        TProcessDto tProcessDto = new TProcessDto();
        BeanUtils.copyProperties(byId,tProcessDto);

        return tProcessDto;
    }

    /**
    *@Author 李磊
    *@Description 服务流程修改
    *@Param [tProcessParam, id]
    *@return com.woniu.car.dto.TProcessDto
    **/
    @Override
    @Transactional
    public TProcessDto update(TProcessUpdateParam tProcessUpdateParam, Integer id) throws Exception {

        TProcess byId = this.getById(tProcessUpdateParam.getProcessId());

        if (null == byId) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }

        Date date = new Date();
        TProcess process = new TProcess();
        BeanUtils.copyProperties(tProcessUpdateParam,process);
        process.setRawUpdateTime(date);
        process.setUserId(id);

        boolean b = this.updateById(process);

        if (!b) {
            throw new ServiceSException(CommonConstant.UPDATEWRONG);
        }

        TProcess tProcess = this.getById(process.getProcessId());
        TProcessDto tProcessDto = new TProcessDto();
        BeanUtils.copyProperties(tProcess,tProcessDto);

        return tProcessDto;
    }

    /**
    *@Author 李磊
    *@Description 服务流程删除
    *@Param [processId, id]
    *@return com.woniu.car.dto.TProcessDto
    **/
    @Override
    @Transactional
    public TProcessDto delete(Integer processId, Integer id) throws Exception{

        TProcess process = new TProcess();
        process.setProcessId(processId);
        process.setUserId(id);
        process.setStatus(CommonConstant.ONE);

        boolean b = this.updateById(process);

        if (!b) {
            throw new ServiceSException(CommonConstant.DELWRONG);
        }

        TProcess byId = this.getById(processId);
        TProcessDto tProcessDto = new TProcessDto();
        BeanUtils.copyProperties(byId,tProcessDto);

        return tProcessDto;
    }

    @Override
    @Transactional
    public List<TProcessDto> queryByService(Integer serviceId) throws Exception{

        if (null == serviceId || serviceId <= 0) {
            throw new ParamException(CommonConstant.DELNULL);
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status",CommonConstant.ZERO);
        wrapper.eq("service_id",serviceId);
        List list = this.list(wrapper);

        if (null == list || list.size() <= 0) {
            throw new ServiceSException(CommonConstant.RESULTNULL);
        }

        return list;
    }

}
